import { createSSRApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import App from './App.vue'
import 'uno.css'
import './style/main.scss'
import router from './router'
import { setupRouteGuard } from './router/guard'
import { httpRequest } from './api/manage'
import { useUserStore } from '@/stores/user'
import { clearCorruptedStorage } from '@/utils/crypto'

export function createApp() {
  const app = createSSRApp(App)
  const pinia = createPinia()

  // 清理损坏的存储数据
  clearCorruptedStorage()

  // 配置 Pinia 持久化插件
  pinia.use(piniaPluginPersistedstate)

  // 挂载 Pinia
  app.use(pinia)

  // 挂载请求实例到全局
  app.config.globalProperties.$request = httpRequest

  // 先挂载路由
  app.use(router)

  // 再初始化路由守卫
  setupRouteGuard(app)

  return {
    app,
    pinia,
    router, // 导出路由实例
  }
}
